package top.codedance.iotp.common.util.db.mysql;

import top.codedance.iotp.common.util.db.JdbcUtil;
import org.apache.log4j.Logger;

import java.sql.*;
import java.util.Properties;

public class MysqlJDBCUtil implements JdbcUtil {

    private static Logger logger = Logger.getLogger(MysqlJDBCUtil.class);

    private static ConnectionPool connectionPool;
    private  Class<?> clazz;
    private Connection connection = null;

    public MysqlJDBCUtil(Class<?> clazz, Logger logger, Properties properties) {
        this.logger = logger;
        this.clazz = clazz;
        connectionPool = new ConnectionPool(clazz, logger, properties);
    }

    public Connection getConnection() {
        try {
            connection = connectionPool.getConnection();
        } catch (InterruptedException throwables) {
            try {
                if (connection != null) {
                    connection.close();
                    connection = null;
                }
            } catch (SQLException e) {
            }
        }
        return connection;
    }

    public void returnConnection(Connection c) {
        connectionPool.returnConnection(c);
    }

    public void destory() {}

}
